home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / Util.java < prev    next >
Text File  |  1998-08-21  |  5KB  |  161 lines

  1. package symantec.itools.awt.util;
  2.  
  3.  
  4. import java.awt.*;
  5.  
  6.  
  7. /**
  8.  *
  9.  *
  10.  * @version 1.0, Nov 26, 1996
  11.  *
  12.  * @author    Symantec
  13.  *
  14.  */
  15.  
  16. //     04/11/97    LAB    Added the getGraphics(Image image, Component component) function
  17. //     04/20/97    LAB    Added import statements for Image and Component.
  18. //                Fixed a type-o in the getGraphics function I added previously that
  19. //                prevented it from working properly.
  20. //    06/01/97    RKM    Added findComponent method
  21.  
  22. /**
  23.  * An all-static utility class with handy helper methods to retrieve font information.
  24.  */
  25. public class Util
  26. {
  27.     /**
  28.      * Do not use, all-static class.
  29.      */
  30.     public Util() {
  31.     }
  32.  
  33.     /**
  34.      * Determines the height of the font being used by the given
  35.      * graphics context.
  36.      * This is the standard height of a line of text in the font.
  37.      * @param g a graphics context
  38.      * @return the font height, in pixels
  39.     */
  40.     public static int getFontHeight(Graphics g)
  41.     {
  42.         return getFontHeight(g.getFontMetrics());
  43.     }
  44.  
  45.     /**
  46.      * Determines the height of the font specified.
  47.      * This is the standard height of a line of text in the font.
  48.      * @param f the font
  49.      * @return the font height, in pixels
  50.      */
  51.     public static int getFontHeight(Font f)
  52.     {
  53.         return getFontHeight(Toolkit.getDefaultToolkit().getFontMetrics(f));
  54.     }
  55.  
  56.     /**
  57.      * Determines the height of the font given the metrics of that font.
  58.      * This is the standard height of a line of text in the font.
  59.      * @param m the metrics of the font
  60.      * @return the font height, in pixels
  61.      */
  62.     public static int getFontHeight(FontMetrics m)
  63.     {
  64.         return m.getHeight();
  65.     }
  66.  
  67.     /**
  68.      * Determines the width of the given string if it were drawn using the
  69.      * specified graphics context.
  70.      * @param g the graphics context
  71.      * @param s the string to determine the width of
  72.      * @return the width of the string, in pixels
  73.      */
  74.     public static int getStringWidth(Graphics g, String s)
  75.     {
  76.         return getStringWidth(g.getFontMetrics(), s);
  77.     }
  78.  
  79.     /**
  80.      * Determines the width of the given string if it were drawn using the
  81.      * specified font.
  82.      * @param g the font
  83.      * @param s the string to determine the width of
  84.      * @return the width of the string, in pixels
  85.      */
  86.     public static int getStringWidth(Font f, String s)
  87.     {
  88.         return getStringWidth(Toolkit.getDefaultToolkit().getFontMetrics(f), s);
  89.     }
  90.  
  91.     /**
  92.      * Determines the width of the given string if it were drawn in a font
  93.      * with the specified metrics.
  94.      * @param m the font metrics
  95.      * @param s the string to determine the width of
  96.      * @return the width of the string, in pixels
  97.      */
  98.     public static int getStringWidth(FontMetrics m, String s)
  99.     {
  100.         return m.stringWidth(s);
  101.     }
  102.  
  103.     /**
  104.      * Retrieves a default dialog font.
  105.      * In this case the typeface is a plain 12-point ôDialogö.
  106.      * @return the default dialog font
  107.      */
  108.     public static Font getDefaultFont()
  109.     {
  110.         return new Font("Dialog", 12, Font.PLAIN);
  111.     }
  112.  
  113.     //getGraphics
  114.     /**
  115.      * Preserves the font information of a graphics object retrieved from
  116.      * an image so that susequent calls to getFontMetrics through the graphics
  117.      * object will not result in a NullPointerException.
  118.      */
  119.     /**
  120.      * Preserves the font information of a graphics object retrieved from
  121.      * an image. This ensures that susequent calls to getFontMetrics through
  122.      * the graphics object will not result in a NullPointerException.
  123.      * It does this by setting the font in the imageÆs graphics context to
  124.      * the font in the component, as needed.
  125.      *
  126.      * @param image the image with the desired graphics context
  127.      * @param component the component
  128.      * @return a graphics context with a currently set font
  129.      */
  130.     public static Graphics getGraphics(Image image, Component component)
  131.     {
  132.         if(image == null)
  133.             return null;
  134.  
  135.         Graphics graphics = image.getGraphics();
  136.         if(graphics != null && component != null && component.getFont() != null)
  137.         {
  138.             graphics.setFont(component.getFont());
  139.         }
  140.         return graphics;
  141.     }
  142.  
  143.     /**
  144.      * Finds the index of a component in a container.
  145.      * @param container the container to search
  146.      * @param component the component to find
  147.      * @return the zero-relative component index, or -1 if the component is not
  148.      * found in the container
  149.      */
  150.     public static int findComponent(Container container, Component component)
  151.     {
  152.         Component[] components = container.getComponents();
  153.         for (int i = 0;i < components.length;i++)
  154.         {
  155.             if (components[i] == component)
  156.                 return i;
  157.         }
  158.         return -1;
  159.     }
  160. }
  161.